package com.swotxu.screwweb.controller;

import cn.smallbun.screw.core.engine.EngineFileType;
import com.swotxu.screwweb.common.to.R;
import com.swotxu.screwweb.service.ScrewService;
import com.swotxu.screwweb.util.IOUtil;
import com.swotxu.screwweb.util.ScrewUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Date: 2021/7/12 15:02
 * @Author: xu.hai
 */
@RestController
public class ScrewController {

    private static final Logger log = LoggerFactory.getLogger(ScrewController.class);

    @Autowired
    private ScrewService screwService;

    @GetMapping("/reload")
    public R reload() {
        screwService.reloadFile();
        return R.ok();
    }

    @GetMapping("/viewOnline")
    public void viewOnline(HttpServletResponse response, @RequestParam(name = "dbKey") String dbKey) {
        screwService.docStreamProcess(response, dbKey, EngineFileType.HTML, IOUtil.FILE_VIEW_ONLINE);
    }

    @GetMapping("/downLoad")
    public void downLoad(HttpServletResponse response, @RequestParam(name = "dbKey") String dbKey, @RequestParam(name = "fileType") String fileType) throws IOException {
        screwService.docStreamProcess(response, dbKey, ScrewUtil.getEngineFileType(fileType), IOUtil.FILE_DOWN_LOAD);
    }
}
